package com.cjj.mvpsample.presenter;

import java.lang.ref.WeakReference;

/**
 * Presenter 基类
 * @Author：Created by cjj on 16/8/22.
 * @Blog：jianjunchen.net
 */
public class BasePresenter<T> {

    private WeakReference<T> wrf = null;//VIEW生命周期结束解耦用

    /**
     * 绑定View
     * @param view
     */
    public void attachView(T view){
        wrf = new WeakReference<T>(view);
    }

    /**
     * 解绑View
     */
    public void detachView(){
        wrf.clear();
        wrf = null;
    }

    /**
     * 获取绑定的View(activity/fragment)
     * @return
     * @throws NullPointerException
     */
    public T getView() throws NullPointerException{
        if (null==wrf||null==wrf.get())
             throw new NullPointerException("trying to use a recycled view");
        return wrf.get();
    }

}
